var mongoose = require('../../models/model');
var ArticleModel = require('../../models/article');

exports.index = function(req,res){

    ArticleModel.find({},function(err,artices){
        res.render('admin/article_list',{ title: 'Tomorrow Blog' ,artices:artices});
    });
};

exports.post = function(req,res){
    res.render('admin/article_post',{ title: 'Tomorrow Blog' });
};

exports.doPost = function(req,res){

    var data = {
        cat_id : req.body.cat_id,
    	title : req.body.title,
    	content : req.body.content,
        //post_user : req.session.user._id,
        author : req.body.author,
        status : req.body.status,
        tags : req.body.tags,
        intro : req.body.intro,
        top_type : req.body.top_type,
        allow_comment : req.body.allow_comment,
        post_time : req.body.post_time
    };

    var ArticleEntity = new ArticleModel(data);
    ArticleEntity.save(function(err){
    	if(err){
    		console.log(err);
    	}else{
            console.log('发布成功');
        }
    	return res.redirect('/admin/article/post');
    });
};

exports.delete = function(req,res){
    var article_id = req.params.id;
    if(article_id){
        ArticleModel.remove({_id:article_id},function(err){
            if(err){
                console.log(err);
            }
            return res.redirect('/admin/article');
        });
    }
}

exports.update = function(req,res){
    var article_id = req.params.id;
    if(article_id){
        //ArticleModel.findById(article_id,function(err,data){
        //    console.log(data);
        //    return res.redirect('/admin/article');
        //});
    }
}

exports.doUpdate = function(req,res){

}